package App.Repository;

import App.Model.Task.CrmTaskSchedule;

import java.sql.*;
import java.util.*;

/**
 * Created by xw on 2019/4/22.
 */
public class TaskAuthRepository {

    public Connection getConnection(String url, String username, String password) {
        try {
            return DriverManager.getConnection(url, username, password);
        } catch (SQLException ex) {
            return null;
            // throw ex;
        }
    }

    public List<CrmTaskSchedule> passUserAuth(String url, String username, String password) throws Exception {

        List<CrmTaskSchedule> _scheduleList = new ArrayList<>();
        Connection conn = null;
        try {
            conn = getConnection(url, username, password);
            Statement stmt = conn.createStatement();
            String _strSql = " select recid,userid,lawoffice,authtype from crm_user_auth where passtype=0 and  TIMESTAMPDIFF(MINUTE,createtime,NOW())  ";
            ResultSet rs = stmt.executeQuery(_strSql);

            ResultSetMetaData metadata = rs.getMetaData();
            while (rs.next()) {
                String _recid = rs.getString("recid");
                int _userid= rs.getInt("userid");
                String _lawOffice=rs.getString("lawoffice");
                int _authtype=rs.getInt("authtype");


                PreparedStatement st = conn.prepareStatement(" update crm_user_auth set passtype=1 where recid= ? ");
                st.setString(1, _recid);
                st.executeUpdate();

                PreparedStatement _updateUserData = conn.prepareStatement(" update crm_user set company=?,title=? where id= ? ");
                _updateUserData.setString(1, _lawOffice);
                _updateUserData.setInt(2, _authtype);
                _updateUserData.setInt(3, _userid);
                _updateUserData.executeUpdate();

            }
            rs.close();
            conn.close();

        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        } finally {
            conn.close();
        }
        return _scheduleList;
    }


}
